<template>
  <div>
    <el-dialog
      title="账号登录"
      :visible.sync="dialogFormVisible"
      width="500px"
      center
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form">
        <el-form-item label="登录账号" :label-width="formLabelWidth">
          <el-input
            v-model="form.tel"
            autocomplete="off"
            placeholder="请输入您的手机号"
            @keyup.enter.native="Sign()"
          ></el-input>
        </el-form-item>
        <el-form-item label="登录密码" :label-width="formLabelWidth">
          <el-input
            v-model="form.pwd"
            autocomplete="off"
            show-password
            placeholder="请输入您的密码"
            @keyup.enter.native="Sign()"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="CancelSign()">取 消</el-button>
        <el-button type="primary" @click="Sign()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Sign",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // dialogFormVisible: false,
      form: {
        tel: "",
        pwd: "",
      },
      initPhoto: require("../assets/images/init.png"),
      formLabelWidth: "120px",
    };
  },
  methods: {
    CancelSign() {
      this.$emit("closepop");
    },
    Sign() {
      if (!this.form.tel) {
        this.$message({
          message: "账号不能为空",
          type: "warning",
        });
        return false;
      }
      if (!this.form.pwd) {
        this.$message({
          message: "密码不能为空",
          type: "warning",
        });
        return false;
      }
      this.$axios({
        url: "/User/GetUserInfoByLogin",
        method: "post",
        data: {
          tel: this.form.tel,
          pwd: this.form.pwd,
        },
        transformRequest: [
          function (data) {
            let ret = "";
            for (let it in data) {
              ret +=
                encodeURIComponent(it) +
                "=" +
                encodeURIComponent(data[it]) +
                "&";
            }
            return ret;
          },
        ],
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
      })
        .then((res) => {
          // console.log(res);
          if (res.data.status == -10003) {
            this.$message({
              message: "账号或密码错误，请重新输入",
              type: "error",
            });
            this.form.pwd = "";
            return false;
          }
          if (res.data.status == 1) {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            if (!res.data.data[0].Photo) {
              res.data.data[0].Photo = this.initPhoto;
            }
            setTimeout(() => {
              this.$cookies.set("userdata", res.data.data[0].UserData);
              this.$cookies.set("userinfo", res.data.data[0]);
              this.$emit("closepop");
              this.$router.go(0);
            }, 500);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style></style>
